package com.dddpeter.app.entity;

import com.baomidou.mybatisplus.annotation.*;
import lombok.Data;
import lombok.NoArgsConstructor;
import lombok.AllArgsConstructor;

import java.time.LocalDateTime;

/**
 * 包含统计信息的文章实体类
 * 用于返回带有热度和评论数的文章列表
 */
@TableName("articles")
@Data
@NoArgsConstructor
@AllArgsConstructor
public class ArticleWithStats {
    
    @TableId(type = IdType.ASSIGN_ID)
    private Long id;
    
    @TableField("title")
    private String title;
    
    @TableField("content")
    private String content;
    
    @TableField("author_id")
    private Long authorId;
    
    @TableField("status")
    private Article.ArticleStatus status = Article.ArticleStatus.DRAFT;
    
    @TableField("categories")
    private String categories;  // 文章分类，多个用逗号分隔
    
    @TableField(value = "created_at", fill = FieldFill.INSERT)
    private LocalDateTime createdAt;
    
    @TableField(value = "updated_at", fill = FieldFill.INSERT_UPDATE)
    private LocalDateTime updatedAt;
    
    // 统计字段
    private Integer likeCount = 0;     // 点赞数
    private Integer commentCount = 0;  // 评论数
    
    /**
     * 从Article实体转换为ArticleWithStats
     */
    public static ArticleWithStats fromArticle(Article article) {
        ArticleWithStats stats = new ArticleWithStats();
        stats.setId(article.getId());
        stats.setTitle(article.getTitle());
        stats.setContent(article.getContent());
        stats.setAuthorId(article.getAuthorId());
        stats.setStatus(article.getStatus());
        stats.setCategories(article.getCategories());
        stats.setCreatedAt(article.getCreatedAt());
        stats.setUpdatedAt(article.getUpdatedAt());
        stats.setLikeCount(0);
        stats.setCommentCount(0);
        return stats;
    }
    
    /**
     * 转换为Article实体
     */
    public Article toArticle() {
        Article article = new Article();
        article.setId(this.id);
        article.setTitle(this.title);
        article.setContent(this.content);
        article.setAuthorId(this.authorId);
        article.setStatus(this.status);
        article.setCategories(this.categories);
        article.setCreatedAt(this.createdAt);
        article.setUpdatedAt(this.updatedAt);
        return article;
    }
}
